package com.onlinewebgames.tictactoe.app;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteException;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.onlinewebgames.tictactoe.R;
import com.onlinewebgames.tictactoe.database.TicTacToeDAO;
import com.onlinewebgames.tictactoe.database.TicTacToeHelper;
import com.onlinewebgames.tictactoe.facebook.utils.LoginButton;
import com.onlinewebgames.tictactoe.models.LoggedUser;
import com.onlinewebgames.tictactoe.provider.UserContentProvider;
import com.onlinewebgames.tictactoe.provider.UserContentProvider.Usuarios;


public class Activity_Main extends Activity {
	/** Called when the activity is first created. */
	
	LoggedUser user;
	
	//content provider
	String[] projection = new String[] {
			Usuarios._ID,
			Usuarios.COL_USERNAME, 
			Usuarios.COL_PASSWORD,
			Usuarios.COL_NAME
			
	};

	
	Dialog diag;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		
		

	}

	public void play(View botonPlay) {
		
 		
	
		Uri usuariosUri = UserContentProvider.CONTENT_URI;
			
		final ContentResolver cr = getContentResolver();
		
		user = TicTacToeHelper.transformToUser(cr.query(usuariosUri, projection, null, null, null));
		
		if(user == null) {
		
			
					RegistrarUsuario();

		}

		else {
				
			Intent intent = new Intent(Activity_Main.this, Activity_EstadoPartidas.class);
			
			Bundle bundle = new Bundle();
			bundle.putSerializable("loggedUser", user);
			intent.putExtras(bundle);
			
			startActivity(intent);
		
		}
		
		
	}
	
	
	
	public void RegistrarUsuario() {
	
			
		final ContentResolver cr = getContentResolver();
		
		LayoutInflater inflater = (LayoutInflater) getApplicationContext()
				.getSystemService(LAYOUT_INFLATER_SERVICE);
		View layout = inflater.inflate(R.layout.registrar,
				(ViewGroup) findViewById(R.id.layout_root));

		Button button_aceptar = (Button) layout
				.findViewById(R.id.button_aceptar);
		Button button_cancelar = (Button) layout
				.findViewById(R.id.button_cancelar);

		final TextView userView = (TextView) layout
				.findViewById(R.id.usuario);
		final TextView passView = (TextView) layout
				.findViewById(R.id.password);

		button_aceptar.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View botonAceptarPopUp) {

				
				cr.delete(UserContentProvider.CONTENT_URI, null, null);
				
				
				ContentValues values = new ContentValues(); 
				
				String username= userView.getText().toString();
				String password= passView.getText().toString();
				String name= "prueba";
				
				values.put(Usuarios.COL_USERNAME, username);
				values.put(Usuarios.COL_PASSWORD, password);
				values.put(Usuarios.COL_NAME, name);
				
				ContentResolver cr = getContentResolver();
							
				
				cr.insert(UserContentProvider.CONTENT_URI, values);

				user = new LoggedUser();
				user.setUser(username);
				user.setPass(password);
				user.setName(name);
				
				diag.cancel();
				

				Intent intent = new Intent(Activity_Main.this, Activity_EstadoPartidas.class);
				
				Bundle bundle = new Bundle();
				bundle.putSerializable("loggedUser", user);
				intent.putExtras(bundle);
				
				startActivity(intent);

			}
		});

		button_cancelar.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				diag.cancel();

			}
		});

		diag = new AlertDialog.Builder(Activity_Main.this).setView(layout)
				.setInverseBackgroundForced(true).create();

		diag.setCancelable(true);
		diag.show();
	
		

		
	}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		
		
		MenuItem menu1=menu.add(0, 1, 1, "Cambiar usuario");
		MenuItem menu2=menu.add(0,2,1,"Cambiar preferencias");
		
		menu1.setIcon(R.drawable.cambiar_user);
		menu2.setIcon(R.drawable.cambiar_preferencias);
		
		return true;
		
	}

	
	
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		
		switch (item.getItemId()) {
		case 1:
			
			RegistrarUsuario();
			break;

		case 2:
			
			break;
		}
		
		
		return super.onOptionsItemSelected(item);
	}

	public void requestPreferences(View view) {

		final Intent intent = new Intent(this, Activity_Preferences.class);
		startActivity(intent);

	}
}